package com.nanyang.tms.service.forecast.impl.flow;

import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.convert.TransConvert;
import com.nanyang.common.core.flow.factory.AbstranctTransPipelineFactory;
import com.nanyang.common.core.flow.invoker.DefaultTransPipeline;
import com.nanyang.common.core.flow.invoker.TransPipeline;
import com.nanyang.tms.api.dto.request.forecast.ForecastInfoAddRequest;
import com.nanyang.tms.domain.forecast.context.ForecastAddContext;
import com.nanyang.tms.manager.forecast.handler.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ForecastProcessPipelineFactory  extends AbstranctTransPipelineFactory<ForecastInfoAddRequest>{
    @Autowired
	private CalculateWeightHandler calculateWeightHandler;
    @Autowired
    private CalculateFreightHandler calculateFreightHandler;
    @Autowired
    private SaveForecastHandler saveForecastHandler;
    @Autowired
    private TransactionRecordHandler transactionRecordHandler;
    @Autowired
    private GetTicketNumberHandler getTicketNumberHandler;
    @Autowired
    private  FreezeHandler freezeHandler;
	@Autowired
	private SendPrintTagHandler sendPrintTagHandler;
	@Override
	public TransHandlerContext createContext() {
		return new ForecastAddContext();
	}

	@Override
	public void doBuild(TransPipeline pipeline) {
		pipeline.addLast(calculateWeightHandler); //无事务的handler
		pipeline.addLast(calculateFreightHandler);//无事务的handler
		pipeline.addLast(false,freezeHandler);//pipeline.addLast(true,freezeHandler)  有事务 并且会提交 ，pipeline.addLast(false,freezeHandler) 有事务但是不提交
		pipeline.addLast(false,saveForecastHandler);
		pipeline.addLast(false,transactionRecordHandler);
		pipeline.addLast(true,getTicketNumberHandler);//无事务的handler
		pipeline.addLast(true,sendPrintTagHandler);//无事务的handler

	}

	@Override
	public TransPipeline createPipeline(TransHandlerContext context) {
		 //所有handler 都没有事务 用这个构造函数
		//return new DefaultTransPipeline(context);
         //所有handler中有一个有事务就需要用 这个构造函数
		return new DefaultTransPipeline(context,super.getDataSourceTransactionManager(),super.getTransactionDefinition()); 
	}

	@Override
	public TransConvert createConvert() {
		return new ForecastAddConvert();
	}

}
